<template>
  <div class="order">
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入订单Id" v-model="oId" type="number">
            <el-button slot="append" icon="el-icon-search" @click="queryOid(oId)"></el-button>
          </el-input>
          <!-- <el-button type="btn" @click="queryOid(oId)">打开嵌套表格的 Dialog</el-button> -->

          <el-dialog title="收货地址" :modal="false" :visible.sync="dialogTableVisible">
            <el-table :data="gridData">
              <el-table-column property="order_id" label="订单id"></el-table-column>
              <el-table-column property="user_id" label="用户id"></el-table-column>
              <el-table-column property="order_number" label="订单编号" width="200"></el-table-column>
              <el-table-column property="order_price" label="订单价格"></el-table-column>
              <el-table-column property="is_send" label="发货状态"></el-table-column>
              <el-table-column property="order_fapiao_title" label="发票信息"></el-table-column>
              <el-table-column property="create_time" label="创建时间"></el-table-column>
              <el-table-column property="pay_status" label="订单状态"></el-table-column>
            </el-table>
          </el-dialog>
        </el-col>
      </el-row>
      <!-- 查询结果 -->

      <!-- 订单列表数据 -->
      <el-table :data="orderlist" border stripe>
        <el-table-column label="订单ID" prop="order_id"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款" prop="pay_status">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status==='1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 订单状态按钮-->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="orderstatusBox(scope.row)"
            ></el-button>
            <!--修改地址按钮-->
            <el-button
              type="success"
              icon="el-icon-location"
              size="mini"
              @click="showBox(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 订单状态对话框 -->
    <el-dialog title="订单状态" :visible.sync="orderstatusVisible" width="50%" :modal="false">
      <el-form :model="orderstatusForm" label-width="100px">
        <el-form-item label="ID">
          <el-input disabled v-model="orderstatusForm.order_id"></el-input>
        </el-form-item>
        <el-form-item label="发货状态">
          <el-input disabled v-model="orderstatusForm.is_send"></el-input>
        </el-form-item>
        <el-form-item label="订单价格">
          <el-input v-model="orderstatusForm.order_price"></el-input>
        </el-form-item>
        <el-form-item label="订单数量">
          <el-input disabled v-model="orderstatusForm.order_number"></el-input>
        </el-form-item>
        <el-form-item label="支付状态">
          <el-input disabled v-model="orderstatusForm.pay_status"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="orderstatusVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendOrderapi">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改地址的对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressVisible"
      width="50%"
      @close="addressDialogClosed"
      :modal="false"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityData" v-model="addressForm.address1"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 
<script>
import { getOrderListapi, orderstatusapi, queryOidapi } from "@/api/orders.js";

export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      oId: "",
      gridData: [],
      dialogTableVisible: false,
      //总数据条数
      total: 0,
      orderlist: [],
      addressVisible: false,
      addressForm: {
        address1: [],
        address1: ""
      },
      addressFormRules: {
        address1: [
          {
            required: true,
            message: "请选择省市区/县",
            trigger: "blur"
          }
        ],
        address2: [
          {
            required: true,
            message: "请填写详细地址",
            trigger: "blur"
          }
        ]
      },
      cityData: [
        {
          value: "beijing",
          label: "北京",
          children: [
            {
              value: "dongchengqu",
              label: "东城区"
            },
            { label: "西城区" },
            { label: "崇文区" },
            { label: "宣武区" },
            { label: "朝阳区" },
            { label: "丰台区" },
            { label: "石景山区" },
            { label: "海淀区" },
            { label: "门头沟区" },
            { label: "房山区" },
            { label: "通州区" },
            { label: "顺义区" },
            { label: "昌平区" },
            { label: "大兴区" },
            { label: "昌平区" },
            { label: "平谷区" },
            { label: "怀柔区" },
            { label: "密云区" },
            { label: "延庆区" }
          ]
        },
        {
          label: "天津",
          children: [
            {
              label: "东城区"
            },
            { label: "和平" },
            { label: "河东" },
            { label: "河西" },
            { label: "南开" },
            { label: "河北" },
            { label: "红桥" },
            { label: "塘沽" },
            { label: "汉沽" },
            { label: "大港" },
            { label: "东丽" },
            { label: "西青" },
            { label: "津南" },
            { label: "北辰" },
            { label: "宁河" },
            { label: "武清" },
            { label: "静海" },
            { label: "宝坻" },
            { label: "蓟县" }
          ]
        },

        {
          label: "河北",
          children: [
            { label: "石家庄" },
            { label: "唐山" },
            { label: "秦皇岛" },
            { label: "邢台" },
            { label: "保定" },
            { label: "张家口" },
            { label: "沧州" },
            { label: "廊坊" },
            { label: "汉沽" },
            { label: "衡水" }
          ]
        },
        {
          label: "山西",
          children: [
            {
              label: "太原"
            },
            { label: "大同" },
            { label: "阳泉" },
            { label: "长治" },
            { label: "晋城" },
            { label: "朔州" },
            { label: "晋中" },
            { label: "运城" },
            { label: "忻州" },
            { label: "临汾" }
          ]
        },
        {
          label: "内蒙古",
          children: [
            {
              label: "呼和浩特"
            },
            { label: "包头" },
            { label: "乌海" },
            { label: "赤峰" },
            { label: "通辽" },
            { label: "鄂尔多斯" },
            { label: "呼伦贝尔" },
            { label: "巴彦淖尔" },
            { label: "乌兰察布" },
            { label: "大兴安港" },
            { label: "锡林郭勒" },
            { label: "阿拉善" }
          ]
        },
        {
          label: "辽宁",
          children: [
            {
              label: "东城区"
            },
            { label: "沈阳" },
            { label: "大连" },
            { label: "鞍山" },
            { label: "抚顺" },
            { label: "本溪" },
            { label: "丹东" },
            { label: "锦州" },
            { label: "营口" },
            { label: "阜新" },
            { label: "辽阳" },
            { label: "盘锦" },
            { label: "铁岭" },
            { label: "朝阳" },
            { label: "葫芦岛" }
          ]
        },
        {
          label: "吉林",
          children: [
            {
              label: "长春"
            },
            { label: "吉林" },
            { label: "四平" },
            { label: "辽源" },
            { label: "通化" },
            { label: "白山" },
            { label: "松原" },
            { label: "白城" },
            { label: "延边" }
          ]
        },
        {
          label: "黑龙江",
          children: [
            {
              label: "哈尔滨"
            },
            { label: "齐齐哈尔" },
            { label: "鸡西" },
            { label: "鹤岗" },
            { label: "双鸭山" },
            { label: "大庆" },
            { label: "伊春" },
            { label: "佳木斯" },
            { label: "七台河" },
            { label: "牡丹江" },
            { label: "黑河" },
            { label: "绥化" },
            { label: "大兴安岭" }
          ]
        },
        {
          label: "上海",
          children: [
            {
              label: "黄浦"
            },
            { label: "卢湾" },
            { label: "徐汇" },
            { label: "长宁" },
            { label: "静安" },
            { label: "普陀" },
            { label: "闸北" },
            { label: "虹口" },
            { label: "杨浦" },
            { label: "闵行" },
            { label: "宝山" },
            { label: "嘉定" },
            { label: "浦东新" },
            { label: "金山" },
            { label: "松江" },
            { label: "南汇" },
            { label: "奉贤" },
            { label: "青浦" },
            { label: "崇明" }
          ]
        },
        {
          label: "江苏",
          children: [
            {
              label: "南京"
            },
            { label: "无锡" },
            { label: "徐州" },
            { label: "常州" },
            { label: "苏州" },
            { label: "南通" },
            { label: "连云港" },
            { label: "淮安" },
            { label: "盐城" },
            { label: "镇江" },
            { label: "泰州" },
            { label: "宿迁" }
          ]
        },
        {
          label: "浙江",
          children: [
            {
              label: "杭州"
            },
            { label: "宁波" },
            { label: "温州" },
            { label: "嘉兴" },
            { label: "湖州" },
            { label: "绍兴" },
            { label: "金华" },
            { label: "衢州" },
            { label: "舟山" },
            { label: "台州" },
            { label: "宝山" },
            { label: "丽水" }
          ]
        },
        {
          label: "安徽",
          children: [
            {
              label: "合肥"
            },
            { label: "芜湖" },
            { label: "蚌埠" },
            { label: "淮南" },
            { label: "马鞍山" },
            { label: "淮北" },
            { label: "铜陵" },
            { label: "安庆" },
            { label: "黄山" },
            { label: "滁州" },
            { label: "阜阳" },
            { label: "宿州" },
            { label: "巢湖" },
            { label: "六安" },
            { label: "亳州" },
            { label: "池州" },
            { label: "宣城" }
          ]
        },
        {
          label: "还有好多"
        },
        {
          label: "一个一个"
        },
        {
          label: "复制手酸"
        }
      ],
      orderstatusVisible: false,
      orderstatusForm: {
        id: "",
        is_send: "",

        order_price: "",
        order_number: "",
        pay_status: ""
      }
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    // 请求接口的时候使用
    getOrderList() {
      //请求数据
      getOrderListapi(this.queryInfo, this.total).then(res => {
        // console.log(res);
        this.orderlist = res.data.goods;
        this.total = res.data.total;
      });
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrderList();
    },
    handleCurrentChange(newPaage) {
      this.queryInfo.pagenum = newPaage;
      this.getOrderList();
    },
    //展示修改地址的对话框
    showBox() {
      this.addressVisible = true;
    },
    //关闭对话框 清除里面的内容
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields();
    },

    orderstatusBox(row) {
      // console.log(this.gridData);

      this.orderstatusForm = {
        order_id: row.order_id,
        is_send: row.is_send,
        order_price: row.order_price,
        order_number: row.order_number,
        pay_status: row.pay_status
      };
      this.orderstatusVisible = true;
    },
    sendOrderapi() {
      this.orderstatusVisible = false;
      let id = this.orderstatusForm.order_id;
      let params = {
        is_send: `${this.orderstatusForm.is_send}`,
        order_price: this.orderstatusForm.order_price,
        order_number: `${this.orderstatusForm.order_number}`,
        pay_status: `${this.orderstatusForm.pay_status}`
      };
      orderstatusapi(id, params).then(res => {
        // console.log(params);
        if (res.meta.status == 201 || res.meta.status == 200) {
          this.getOrderList();
          this.$message.success("更新成功");
        } else {
          this.$message.error("更新失败哦~~");
        }
      });
    },
    //查询通过id
    queryOid(oId) {
      let id = Number(oId);
      queryOidapi(id).then(res => {
        if (res.meta.status == 201 || res.meta.status == 200) {
          this.gridData.splice(0, 1, res.data);
          this.gridData[0].create_time = this.timeConvert(
            this.gridData[0].create_time,
            1
          );
          // console.log(this.timeConvert(this.gridData[0].create_time, 1));
          this.dialogTableVisible = true;
          this.$message.success("查询成功");
        } else {
          this.$message.error("id不存在，请重新输入");
          this.gridData.splice(0, 1);
          this.dialogTableVisible = false;
        }
      });
    },
    timeConvert(timestamp, num) {
      //num:0 YYYY-MM-DD  num:1  YYYY-MM-DD hh:mm:ss // timestamp:时间戳
      timestamp = timestamp + "";
      timestamp = timestamp.length == 10 ? timestamp * 1000 : timestamp;
      var date = new Date(timestamp);
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      var d = date.getDate();
      d = d < 10 ? "0" + d : d;
      var h = date.getHours();
      h = h < 10 ? "0" + h : h;
      var minute = date.getMinutes();
      var second = date.getSeconds();
      minute = minute < 10 ? "0" + minute : minute;
      second = second < 10 ? "0" + second : second;
      if (num == 0) {
        return y + "-" + m + "-" + d;
      } else {
        return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
      }
    }
  }
};
</script>
 
<style scoped lang = "scss">
.order {
  width: 100%;
  height: 100%;
}
.el-cascader {
  width: 100%;
}
.el-breadcrumb {
  margin-left: 20px;
}
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 12px;
}

.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 12px;
}

.el-card {
  height: 100% !important;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
}

.el-table {
  margin-top: 15px;
  font-size: 12px;
}

.el-pagination {
  margin-top: 15px;
  margin-left: 100px;
}
</style>